package com.bjmashibing.system.huawei;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @Classname fun_cmd
 * @Description TODO
 * @Date 2020/8/31/031 19:25
 * @Created by Administrator
 */
public class Cmd_fun {
    public static String ip_port = "";
    public static int j = 0;
    private static int i = 0;
    private static byte[] array_temp = new byte[1460];
    private static byte[] array_back = new byte[9];
    private static byte[] array_front = new byte[9];
    private static byte[] array_1 = new byte[1];
    private static byte[] array_3 = new byte[3];
    private static byte[] array_temp_1 = new byte[1460];
    private static byte[] array_temp_2 = new byte[1460];

    public static ByteBuffer buffer;


    private final static Logger logger = LoggerFactory.getLogger(Cmd_fun.class);
    String www = "dfd";
    public static int big_class_2 = 255;
    public static int small_class_2 = 255;
    public static void print_cmd(byte[] cmd) {
        try{
//            logger.info("aaaaaaaaaaa");
//            Cmd_fun.print_cmd2(cmd,111);
            byte[] buf_len = new byte[5];
            for (int i = 0; i < 5; i++) {
                buf_len[i] = 0;
            }
            int len_std_a = 0;
            int len_std_b = 0;
            if (cmd[7] < 58) {
                len_std_a = cmd[7] - 48;
            } else if ((cmd[7] >= 65) && (cmd[7] <= 70)) {
                len_std_a = cmd[7] - 55;
            }
            if (cmd[8] < 58) {
                len_std_b = cmd[8] - 48;
            } else if ((cmd[8] >= 65) && (cmd[7] <= 70)) {
                len_std_b = cmd[8] - 55;
            }
//            logger.info("bbbbbbbbbbbbbbbbbbb");
            int len_standard = len_std_a * 16 + len_std_b;
//            logger.info("len_standard=" + len_standard);
            int len_pri = cmd[len_standard + 10] * 256
                    +  (int)(cmd[len_standard + 10 + 1] & 0xff);
//                    +  (int)(cmd[len_standard + 10 + 1]);
//            logger.info("len_pri=" + len_pri);
            int len_total = len_standard + len_pri + 10 + 2 + 4;
//            logger.info("len_total=" + len_total);
            // printf("len_standard=%d,len_pri=%d,len_total=%d\n",len_standard,len_pri,len_total);
//            logger.info("www=" + len_standard + " " + len_pri + " " + len_total);
            int len_temp = len_standard + 31 + 10;
            // ////////////////////////////////////////////////////////////////////////////////

            int big_class = cmd[len_temp];
            int small_class = cmd[len_temp + 1];
            big_class_2 = big_class;
            small_class_2 = small_class;
            String str_ip_event = "";
            byte[] cchar_temp = new byte[len_standard + 10];
            System.arraycopy(cmd, 0, cchar_temp, 0, cchar_temp.length);
            for (int i = 1; i < cchar_temp.length - 1; i++) {
                // logger.info(cchar[i]+"-->"+(int)cchar[i]);
                str_ip_event = str_ip_event + (char) cchar_temp[i];
            }

            // /////////////////////////////////////////////////////////////////////////////////
//            logger.info("bigclass=0x" + Cmd_fun.bytesToHexString(cmd[len_temp]) + ",small_class=0x"
//					+ Cmd_fun.bytesToHexString(cmd[len_temp + 1]));
            String str_event = str_ip_event;
            for (int i = len_standard + 10; i < len_total; i++) {
                str_event = str_event + Cmd_fun.bytesToHexString(cmd[i]) + " ";
            }
//            logger.info( "str_ip_event=" + str_ip_event);
//            logger.info("print_cmd=" + str_event);

            logger.info("print_cmd,str_event=" + str_event);

        }
        catch(Exception e){
            logger.info("print_cmd,,,,,,,,,,,,,exception error");
        }

    }

    public static void print_cmd2(byte[] cmd,int len) {
        try{
            logger.info("aaaaaaaaaaa");
            String str_ip_event = "";
            for (int i = 0; i < len; i++) {

                str_ip_event = str_ip_event + Cmd_fun.bytesToHexString(cmd[i]) + " ";
            }

            String str_event = str_ip_event;

            logger.info("data_all=" + str_event);
        }
        catch(Exception e){
            logger.info("print_cmd,,,,,,,,,,,,,exception error");
        }

    }

    public static void print_cmd_hex(byte[] cmd, int len) {
        try{
            String data_hex = "";
            for (int i = 0; i < len; i++) {
                data_hex = data_hex + Cmd_fun.bytesToHexString(cmd[i]) + " ";
            }
            logger.info("data_hex=" + data_hex);
        }
        catch(Exception e){
            logger.info("print_cmd_hex,,,,,,,,,,,,,exception error");
        }

    }

    public static String bytesToHexString(byte src) {

        try{
            StringBuilder stringBuilder = new StringBuilder("");
            int v = src & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);

            return stringBuilder.toString();
        }
        catch(Exception e){
            logger.info("bytesToHexString,,,,,,,,,,,,,exception error");
            return "";
        }

    }

    public static boolean exception_handle_cmd_format_detail(byte[] cmd) {
        try{
            byte[] buf_len = new byte[5];
            for (int i = 0; i < 5; i++) {
                buf_len[i] = 0;
            }
            int len_std_a = 0;
            int len_std_b = 0;
            if (cmd[7] < 58) {
                len_std_a = cmd[7] - 48;
            } else if ((cmd[7] >= 65) && (cmd[7] <= 70)) {
                len_std_a = cmd[7] - 55;
            }
            if (cmd[8] < 58) {
                len_std_b = cmd[8] - 48;
            } else if ((cmd[8] >= 65) && (cmd[7] <= 70)) {
                len_std_b = cmd[8] - 55;
            }
            if(len_std_a < 0){
                logger.info("len_std_a=" + len_std_a);
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            if(len_std_b < 0){
                logger.info("len_std_b=" + len_std_b);
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            int len_standard = len_std_a * 16 + len_std_b;
            if((len_standard > 115) || (len_standard < 50))
            {
                logger.info("exception_handle_cmd_format_detail,len_standard=" + len_standard);
                logger.info("error cmd,.....................................!");
                return true;

            }
            int len_pri = cmd[len_standard + 10] * 256 + (int)(cmd[len_standard + 10 + 1] & 0xff);
            if((len_pri > 1024) || (len_pri < 31))
            {
                logger.info("exception_handle_cmd_format_detail,len_pri=" + len_pri);
                logger.info("error cmd,.....................................!");
                return true;

            }
            int len_total = len_standard + len_pri + 10 + 2 + 4;

            // logger.info("www=" + len_standard+ " " + len_pri+ " " + len_total);
            int len_temp = len_standard + 31 + 10;
            // ////////////////////////////////////////////////////////////////////////////////

            int big_class = cmd[len_temp];
            int small_class = cmd[len_temp + 1];
            long conn_number = ((byte)(cmd[len_temp + 2] & 0xff)) * 65536 + ((byte)(cmd[len_temp + 3] & 0xff)) * 256 + ((byte)(cmd[len_temp + 4] & 0xff));
            if(conn_number > 400000){
                //Cmd_fun.print_cmd(cmd, Main_this);
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("conn_number=" + conn_number + " ,conn_number is too big!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            ////////////////////////////////////////////////////////////////////////////////////////////////
            int place_quote_sencond = find_place_second(cmd,'\"',len_standard + 10);
            //logger.info("place_quote_sencond=" + place_quote_sencond);
            if(place_quote_sencond == -1)
            {
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("exception_handle_cmd_format_detail,id_token's sencond quote was not found");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            /////////////////////////////////////////////////////////
            int place_R = find_place(cmd,'R',len_standard + 10);
            //logger.info("place_R=" + place_R);
            if(place_R == -1)
            {
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("Rrecv was not found!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            ////////////////////////////////////////////////////////
            byte[] buf_id = new byte[7];
            byte[] buf_temp = new byte[1024];
            System.arraycopy(cmd, 10, buf_temp, 0, len_standard);

            System.arraycopy(buf_temp, 0, buf_id, 0, place_quote_sencond - 10);
            String str_buf_id = new String(buf_id);
            //logger.info("str_buf_id =" + str_buf_id);
            //memcpy(buf_id,&cmd[10],place_quote_sencond - 10);
            int place_L = 0;
            if(str_buf_id.equals("NULL")){
                //printf("buf_id=%s\n",buf_id);
                place_L = find_place_L(cmd,'L',len_standard + 10);
                if(place_L == -1)
                {
                    logger.info("big_class=0x=" + Integer.toHexString(big_class)
                            + " small_class=0x=" + Integer.toHexString(small_class));
                    logger.info("Lpref was not found!");
                    logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                    return true;
                }
            }
            else {
                place_L = find_place(cmd,'L',len_standard + 10);
                //logger.info("place_L=" + place_L);
                if(place_L == -1)
                {
                    logger.info("big_class=0x=" + Integer.toHexString(big_class)
                            + " small_class=0x=" + Integer.toHexString(small_class));
                    logger.info("Lpref was not found!");
                    logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                    return true;
                }

            }
            //////////////////////////////////////////////////////////
            int place_well = find_place(cmd,'#',len_standard + 10);
            //logger.info("place_well=" + place_well);
            if(place_well == -1)
            {
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("wellnumber_first was not found!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            int place_bracket_front = find_place(cmd,'[',len_standard + 10);
            //logger.info("place_bracket_front=" + place_bracket_front);
            if(place_bracket_front == -1)
            {
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("place_bracket_front was not found!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            int place_bracket_back = find_place(cmd,']',len_standard + 10);
            //logger.info("place_bracket_back=" + place_bracket_back);
            if(place_bracket_back == -1)
            {
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("place_bracket_back was not found!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            //////////////////////////////////////////////////////////////////
            if(cmd[0] != 0x0A)
            {
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                //logger.info("0x" + Integer.toHexString(big_class) + " 0x"	+ Integer.toHexString(small_class));
                logger.info("ip standard cmd exception,cmd[0] != 0x0A," + "0x" + Integer.toHexString(big_class) + " 0x"	+ Integer.toHexString(small_class));
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            if(cmd[len_standard + 9] != 0x0d)
            {
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("place_quote_sencond=" + place_quote_sencond);
                logger.info("place_R="  + place_R);
                logger.info("place_L="  + place_L);
                logger.info("place_well=" + place_well);
                logger.info("place_bracket_front=" + place_bracket_front);
                logger.info("place_bracket_back=" + place_bracket_back);
                logger.info("len_standard + 9=" + len_standard + 9);
                logger.info("cmd[len_standard + 9]=0x" + Integer.toHexString(cmd[len_standard +10]) + " is not 0x0d,this package is error");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            if(cmd[9] != '\"')
            {
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("cmd[9]=0x" + Integer.toHexString(cmd[9]) + " is not \"_front,this package is error!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            //////////////////////////////////////////////////////////////////////////////////////
            if((place_quote_sencond < 10) || (place_quote_sencond > place_R)
                    || (place_quote_sencond > place_well)
                    || (place_quote_sencond > place_bracket_front) || (place_quote_sencond > place_bracket_back)
                    || (place_quote_sencond > (len_standard + 9))){
//                Cmd_fun.print_cmd( cmd);
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("place_quote_sencond=" + place_quote_sencond);
                logger.info("place_R="  + place_R);
                logger.info("place_L="  + place_L);
                logger.info("place_well=" + place_well);
                logger.info("place_bracket_front=" + place_bracket_front);
                logger.info("place_bracket_back=" + place_bracket_back);
                logger.info("len_standard + 9=" + len_standard + 9);
                logger.info("place_quote_sencond's position is wrong!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            if((place_R < 14) || (place_quote_sencond > place_R)
                    || (place_R > place_well)
                    || (place_R > place_bracket_front) || (place_R > place_bracket_back)
                    || (place_R > (len_standard + 9))){
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("Rrecv's position is wrong!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }

            if((place_well < 26) || (place_quote_sencond > place_well)
                    || (place_R > place_well)
                    || (place_well > place_bracket_front) || (place_well > place_bracket_back)
                    || (place_well > (len_standard + 9))){
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("well_number's position is wrong!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            if((place_bracket_front < 28) || (place_quote_sencond > place_bracket_front)
                    || (place_R > place_bracket_front)
                    || (place_well > place_bracket_front) || (place_bracket_front > place_bracket_back)
                    || (place_bracket_front > (len_standard + 9))){
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("place_bracket_front's position is wrong!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            if((place_bracket_back < 29) || (place_quote_sencond > place_bracket_back)
                    || (place_R > place_bracket_back)
                    || (place_well > place_bracket_back) || (place_bracket_front > place_bracket_back)
                    || (place_bracket_back > (len_standard + 9))){
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("place_bracket_back's position is wrong!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            /////////////////////////////////////////////////////////////////////////////////////////////
            if((place_bracket_front - place_well -1) > 18 ){
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("acct(well_number's back) is too long!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            if((place_bracket_back - place_bracket_front - 1) > 32 ){
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("the content in the bracket is too long!");
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;
            }
            ///////////////////////////////////////////////////////////////////////////////
            if(!((big_class == 0x01)
                    ||  (big_class == 0x02)
                    ||  (big_class == 0x00)
                    ||  (big_class == 0x33)
                    || (big_class == 0x05)
                    ||  (big_class == 0x0b)
                    ||  (big_class == 0x0f)
                    || (big_class == 0x0d)))
            {
                logger.info("big_class=0x=" + Integer.toHexString(big_class)
                        + " small_class=0x=" + Integer.toHexString(small_class));
                logger.info("phone's big_class is wrong,big_class=0x" + Integer.toHexString(big_class));
                logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                return true;

            }
            ////////////////////////////////////////////////////////////////////////////////////////////////
            if(big_class == 0x01)
            {
                if(small_class > 0x04)
                {
                    logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                            + " small_class=0x=" + Integer.toHexString(small_class));
                    logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                    return true;

                }else{
                    int conn_number_phone = cmd[len_temp + 2] * 65536 + cmd[len_temp + 3] * 256 + cmd[len_temp + 4];
                    if(conn_number_phone > 400000){
                        logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                                + " small_class=0x=" + Integer.toHexString(small_class));
                        logger.info("conn_number_phone=" + conn_number_phone + " ,conn_number_phone is too big!");
                        logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                        return true;
                    }
                }

            }
            //////////////////////////////////////////////////////////////////////////////////////////////////////
            if(big_class == 0x02)
            {
                if(small_class != 0x06)
                {
                    logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                            + " small_class=0x=" + Integer.toHexString(small_class));
                    logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                    return true;

                }else{
                    int conn_number_machine = cmd[len_temp + 2] * 65536 + cmd[len_temp + 3] * 256 + cmd[len_temp + 4];
                    if(conn_number_machine > 100000){
                        logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                                + " small_class=0x=" + Integer.toHexString(small_class));
                        logger.info("conn_number_machine=" + conn_number_machine + " ,conn_number_machine is too big!");
                        logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                        return true;
                    }
                }

            }
            /////////////////////////////////////////////////////////////////////////////////////////////////////
            if(big_class == 0x05)
            {
                if(small_class > 0x20)
                {
                    logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                            + " small_class=0x=" + Integer.toHexString(small_class));
                    logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                    return true;

                }else{
                    if(small_class == 0x13){
                        int conn_number_machine = cmd[len_temp + 2] * 65536 + cmd[len_temp + 3] * 256 + cmd[len_temp + 4];
                        if(conn_number_machine > 400000){
                            logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                                    + " small_class=0x=" + Integer.toHexString(small_class));
                            logger.info("conn_number_machine=" + conn_number_machine + " ,conn_number_machine is too big!");
                            logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                            return true;
                        }
                    }else{
                        int conn_number_machine = cmd[len_temp + 2] * 65536 + cmd[len_temp + 3] * 256 + cmd[len_temp + 4];
                        if(conn_number_machine > 100000){
                            logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                                    + " small_class=0x=" + Integer.toHexString(small_class));
                            logger.info("conn_number_machine=" + conn_number_machine + " ,conn_number_machine is too big!");
                            logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                            return true;
                        }


                    }

                    ///////////////////////////////////////////////////////////////////////
                    int conn_number_phone = cmd[len_temp + 5] * 65536 + cmd[len_temp + 6] * 256 + cmd[len_temp + 7];
                    if(conn_number_phone > 400000){
                        logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                                + " small_class=0x=" + Integer.toHexString(small_class));
                        logger.info("conn_number_phone=" + conn_number_phone + " ,conn_number_phone is too big!");
                        logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                        return true;
                    }
                }

            }
            /////////////////////////////////////////////////////////////////////////////////////////////////////
            if(big_class == 0x0b)
            {
                if(small_class > 0x20)
                {
                    logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                            + " small_class=0x=" + Integer.toHexString(small_class));
                    logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                    return true;

                }else{

                    int conn_number_machine = cmd[len_temp + 2] * 65536 + cmd[len_temp + 3] * 256 + cmd[len_temp + 4];
                    if(small_class == 0x0b){
                        if(conn_number_machine > 400000){
                            logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                                    + " small_class=0x=" + Integer.toHexString(small_class));
                            logger.info("conn_number_machine=" + conn_number_machine + " ,conn_number_machine is too big!");
                            logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                            return true;
                        }
                    }else{

                        if(conn_number_machine > 100000){
                            logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                                    + " small_class=0x=" + Integer.toHexString(small_class));
                            logger.info("conn_number_machine=" + conn_number_machine + " ,conn_number_machine is too big!");
                            logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                            return true;
                        }

                    }

                }

            }
            if(big_class == 0x0d)
            {
                if(small_class > 0x01)
                {
                    logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                            + " small_class=0x=" + Integer.toHexString(small_class));
                    logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                    return true;

                }else{
                    int conn_number_machine = cmd[len_temp + 2] * 65536 + cmd[len_temp + 3] * 256 + cmd[len_temp + 4];
                    if(conn_number_machine > 100000){
                        logger.info("phone's small_class is wrong,big_class=0x=" + Integer.toHexString(big_class)
                                + " small_class=0x=" + Integer.toHexString(small_class));
                        logger.info("conn_number_machine=" + conn_number_machine + " ,conn_number_machine is too big!");
                        logger.info("exception_handle_cmd_format_detail,error cmd,.....................................!");
                        return true;
                    }
                }

            }
            //logger.info("right cmd,.....................................!");
            return false;


        }

        catch(Exception e){

            logger.info("exception_handle_cmd_format_detail,,,,,,,,,,,,,exception error");
            return true;
        }

    }

    public static  int find_place_second(byte[] buf,char ch,int len)
    {
        try{
            int count = 0;
            int i;
            for(i = 0;i < len;i++)
            {
                if(buf[i] == ch)
                {
                    count++;
                    if(count == 2){
                        return i;
                    }

                }
                if(count > 2){
                    return -1;
                }
            }
            return -1;
        }
        catch(Exception e){
            logger.info("main,error=",e);
            e.printStackTrace();
        }
        return -1;

    }
    public static  int find_place(byte[] buf,char ch,int len)
    {
        try{
            int i;
            for(i = 0;i < len;i++)
            {
                if(buf[i] == ch)
                {
                    return i;
                }
            }
            return -1;
        }
        catch(Exception e){
            logger.info("main,error=",e);
            e.printStackTrace();
        }
        return -1;


    }
    public static  int find_place_L(byte[] buf,char ch,int len)
    {
        try{
            int count = 0;
            int i;
            for(i = 0;i < len;i++)
            {
                if(buf[i] == ch)
                {
                    count++;
                    if(count == 3){
                        return i;
                    }


                }
                if(count > 2){
                    return -1;
                }
            }
            return -1;
        }
        catch(Exception e){
            logger.info("main,error=",e);
            e.printStackTrace();
        }
        return -1;


    }
    public static boolean  crc_check_std(byte[] cmd,int para)
    {
        try{
            byte[] buf = new byte[5];
            byte[] buf_id = new byte[256];
            byte[] buf_len = new byte[5];
            //printf("buf_id=%s\n",buf_id);
            /////////////////////////////////////////////////////////////////
            int len_std_a = 0;
            int len_std_b = 0;
            if(cmd[7] < 58)
            {
                len_std_a = cmd[7] - 48;
            }
            else if((cmd[7] >= 65) && (cmd[7] <= 70))
            {
                len_std_a = cmd[7] - 55;
            }
            if(cmd[8] < 58)
            {
                len_std_b = cmd[8] - 48;
            }
            else if((cmd[8] >= 65) && (cmd[7] <= 70))
            {
                len_std_b = cmd[8] - 55;
            }
            int len_standard = len_std_a * 16 + len_std_b;
            int len_pri = cmd[len_standard + 10] * 256 + (int)(cmd[len_standard + 10 + 1] & 0xff);
            int len_total = len_standard + len_pri + 10 + 2 + 4;
            //////////////////////////////////////////////////////////////////////////////
            byte[] buf_temp = new byte[1024];
            System.arraycopy(cmd, 9, buf_temp, 0, len_standard + 1);
            System.arraycopy(buf_temp, 0, buf_id, 0, 9);
            //memcpy(buf_id,&cmd[9],9);
            int crc_value2 = udp_cmd.calcCRC_buf(buf_temp, len_standard);
            udp_cmd.NumToHex_4(crc_value2, buf_len);
            //unsigned int crc_value = Cstatic_var::calcCRC_buf(&cmd[9],len_standard);
            //Cstatic_var::NumToHex_4(crc_value,(char *)buf);
		/*logger.info("crc_check_std,init  " + Integer.toHexString(cmd[1]) + "  " + Integer.toHexString(cmd[2]) + "   "
				+ Integer.toHexString(cmd[3]) + "   " + Integer.toHexString(cmd[4]));
		logger.info("crc_check_std,get  " + Integer.toHexString(buf_len[0]) + "  " + Integer.toHexString(buf_len[1]) + "   "
				+ Integer.toHexString(buf_len[2]) + "   " + Integer.toHexString(buf_len[3]));*/

            int len_bigclass = len_standard + 31 + 10;
            int big_class = cmd[len_bigclass];
            int small_class = cmd[len_bigclass + 1];
            int len_temp_crc = len_standard + 31 + 10;
            int conn_number = cmd[len_temp_crc + 2] * 65536 + cmd[len_temp_crc + 3] * 256 + cmd[len_temp_crc + 4];
            //logger.info("conn_number=" + conn_number);
            if((buf_len[0] == cmd[1]) && (buf_len[1] == cmd[2]) && (buf_len[2] == cmd[3]) && (buf_len[3] == cmd[4]))
            {
                if(para == 0)
                {
                    //logger.info("crc check std right... big_class=" + Integer.toHexString(big_class) + ",small_class=" + Integer.toHexString(small_class));
                }
                return true;
            }
            else
            {
                //Cstatic_var::printf_cmd_client(cmd,len_total);
                if(para == 0)
                {
                    logger.info("crc check std error... big_class=" + Integer.toHexString(big_class) + ",small_class=" + Integer.toHexString(small_class));
                }
                return false;
            }
        }
        catch(Exception e){
            logger.info("crc_check_std,error=",e);
            e.printStackTrace();
        }
        return false;

    }
    public static boolean  crc_check_pri(byte[] cmd,int para)
    {
        try{
            byte[] buf = new byte[5];
            byte[] buf_id = new byte[256];
            byte[] buf_len = new byte[5];
            //printf("buf_id=%s\n",buf_id);
            /////////////////////////////////////////////////////////////////
            int len_std_a = 0;
            int len_std_b = 0;
            if(cmd[7] < 58)
            {
                len_std_a = cmd[7] - 48;
            }
            else if((cmd[7] >= 65) && (cmd[7] <= 70))
            {
                len_std_a = cmd[7] - 55;
            }
            if(cmd[8] < 58)
            {
                len_std_b = cmd[8] - 48;
            }
            else if((cmd[8] >= 65) && (cmd[7] <= 70))
            {
                len_std_b = cmd[8] - 55;
            }
            int len_standard = len_std_a * 16 + len_std_b;
            int len_pri = cmd[len_standard + 10] * 256 + (int)(cmd[len_standard + 10 + 1] & 0xff);
            int len_total = len_standard + len_pri + 10 + 2 + 4;
            //////////////////////////////////////////////////////////////////////////////
            byte[] buf_temp = new byte[1024];
            System.arraycopy(cmd, len_standard + 10 + 2, buf_temp, 0, len_pri);

            //memcpy(buf_id,&cmd[9],9);
            int crc_value2 = udp_cmd.calcCRC_buf(buf_temp, len_pri);
            udp_cmd.NumToHex_4(crc_value2, buf_len);
            //unsigned int crc_value = Cstatic_var::calcCRC_buf(&cmd[9],len_standard);
            //Cstatic_var::NumToHex_4(crc_value,(char *)buf);
		/*logger.info("crc_check_pri,init  " + Integer.toHexString(cmd[len_total - 4]) + "  " + Integer.toHexString(cmd[len_total - 3]) + "   "
				+ Integer.toHexString(cmd[len_total - 2]) + "   " + Integer.toHexString(cmd[len_total - 1]));
		logger.info("crc_check_pri,get  " + Integer.toHexString(buf_len[0]) + "  " + Integer.toHexString(buf_len[1]) + "   "
				+ Integer.toHexString(buf_len[2]) + "   " + Integer.toHexString(buf_len[3]));*/

            int len_bigclass = len_standard + 31 + 10;
            int big_class = cmd[len_bigclass];
            int small_class = cmd[len_bigclass + 1];
            int len_temp_crc = len_standard + 31 + 10;
            int conn_number = cmd[len_temp_crc + 2] * 65536 + cmd[len_temp_crc + 3] * 256 + cmd[len_temp_crc + 4];
            //logger.info("conn_number=" + conn_number);
            if((buf_len[0] == cmd[len_total - 4]) && (buf_len[1] == cmd[len_total - 3]) && (buf_len[2] == cmd[len_total - 2]) && (buf_len[3] == cmd[len_total - 1]))
            {
                if(para == 0)
                {
                    //logger.info("crc check pri right... big_class=" + Integer.toHexString(big_class) + ",small_class=" + Integer.toHexString(small_class));
                }
                return true;
            }
            else
            {
                //Cstatic_var::printf_cmd_client(cmd,len_total);
                if(para == 0)
                {
                    logger.info("crc check pri error... big_class=" + Integer.toHexString(big_class) + ",small_class=" + Integer.toHexString(small_class));
                }
                return false;
            }
        }
        catch(Exception e){
            logger.info("main,error=",e);
            e.printStackTrace();
        }
        return false;

    }

    public static void send_44_00(long conn_number,String ip_port) {
        try{

            int[] len_ipevent_public = new int[2];
            byte[] buf_len = new byte[5];
            byte[] buf = new byte[1460];
            int length_private = 29 + 5;
            len_ipevent_public[1] = length_private;
            build_ipevent_publc(len_ipevent_public, buf);
            int len_toacct = len_ipevent_public[0];
            int len_tolevel = len_toacct + 20 + 31 - 7;
            // big calss and small class
            buf[len_tolevel + 8] = 0x44;
            buf[len_tolevel + 9] = 0x00;
            buf[len_tolevel + 10] = (byte) ((conn_number >> 16) & 0xff);
            buf[len_tolevel + 11] = (byte) ((conn_number >> 8) & 0xff);
            buf[len_tolevel + 12] = (byte) (conn_number & 0xff);
            // crc_private
            byte[] buf_3 = new byte[length_private];
            System.arraycopy(buf, len_toacct + 20 + 3, buf_3, 0, length_private);
            int crc_value2 = udp_cmd.calcCRC_buf(buf_3, length_private);
            udp_cmd.NumToHex_4(crc_value2, buf_len);
            for (int i = 0; i < 4; i++) {
                buf[len_tolevel + 13 + i] = buf_len[i];
            }
            try {
//				Cmd_fun.print_cmd(buf);
                send_cmd_to_server(buf,ip_port);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                logger.info("send_44_00,error=",e);
                e.printStackTrace();
            }
        }
        catch(Exception e){
            logger.info("send_44_00,,,,,,,,,,,,,exception error");
        }

    }
    public static void send_44_01(long conn_number,String ip_port,int para) {
        try{

            int[] len_ipevent_public = new int[2];
            byte[] buf_len = new byte[5];
            byte[] buf = new byte[1460];
            int length_private = 29 + 6;
            len_ipevent_public[1] = length_private;
            build_ipevent_publc(len_ipevent_public, buf);
            int len_toacct = len_ipevent_public[0];
            int len_tolevel = len_toacct + 20 + 31 - 7;
            // big calss and small class
            buf[len_tolevel + 8] = 0x44;
            buf[len_tolevel + 9] = 0x01;
            buf[len_tolevel + 10] = (byte) ((conn_number >> 16) & 0xff);
            buf[len_tolevel + 11] = (byte) ((conn_number >> 8) & 0xff);
            buf[len_tolevel + 12] = (byte) (conn_number & 0xff);
            buf[len_tolevel + 13] = (byte) (para & 0x01);
            // crc_private
            byte[] buf_3 = new byte[length_private];
            System.arraycopy(buf, len_toacct + 20 + 3, buf_3, 0, length_private);
            int crc_value2 = udp_cmd.calcCRC_buf(buf_3, length_private);
            udp_cmd.NumToHex_4(crc_value2, buf_len);
            for (int i = 0; i < 4; i++) {
                buf[len_tolevel + 14 + i] = buf_len[i];
            }
            try {
//				Cmd_fun.print_cmd(buf);
                send_cmd_to_server(buf,ip_port);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                logger.info("send_44_00,error=",e);
                e.printStackTrace();
            }
        }
        catch(Exception e){
            logger.info("send_44_00,,,,,,,,,,,,,exception error");
        }

    }
    public static void send_44_04(long conn_number,String ip_port,String str_machine_name) {
        try{

            String str_alarm_events[] = new String[1];
            Integer nums [] = new Integer[1];
            static_var.get_alarm_event_from_map_history(str_machine_name,str_alarm_events,nums);
//            logger.info(str_alarm_events[0]);
//            logger.info("" + nums[0]);
            if (str_machine_name.equals("866569060902449")) {
                logger.info("send_44_04,nums[0]=" + nums[0] + ",str_acct_machine=" + str_machine_name);
//                                Cmd_fun.print_cmd(cmd);
            }
            int[] len_ipevent_public = new int[2];
            byte[] buf_len = new byte[5];
            byte[] buf = new byte[1460];
            int length_private = 29 + 5 + nums[0]*38 + 1;
            len_ipevent_public[1] = length_private;
            build_ipevent_publc_machine_account(len_ipevent_public, buf,str_machine_name);
            int len_toacct = len_ipevent_public[0];
            int len_tolevel = len_toacct + 20 + 31 - 7;
            // big calss and small class
            buf[len_tolevel + 8] = 0x44;
            buf[len_tolevel + 9] = 0x04;
            buf[len_tolevel + 10] = (byte) ((conn_number >> 16) & 0xff);
            buf[len_tolevel + 11] = (byte) ((conn_number >> 8) & 0xff);
            buf[len_tolevel + 12] = (byte) (conn_number & 0xff);





            buf[len_tolevel + 13] = (byte) (nums[0] & 0xff);
            System.arraycopy(str_alarm_events[0].getBytes(),0,buf,len_tolevel + 14,nums[0]*38);

            // crc_private
            byte[] buf_3 = new byte[length_private];
            System.arraycopy(buf, len_toacct + 20 + 3, buf_3, 0, length_private);
            int crc_value2 = udp_cmd.calcCRC_buf(buf_3, length_private);
            udp_cmd.NumToHex_4(crc_value2, buf_len);
            for (int i = 0; i < 4; i++) {
                buf[len_tolevel + 14 + nums[0]*38 + i] = buf_len[i];
            }
            try {
//				Cmd_fun.print_cmd(buf);
                send_cmd_to_server2(buf,ip_port,str_machine_name);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                logger.info("send_44_04,error=",e);
                e.printStackTrace();
            }
        }
        catch(Exception e){
            logger.info("send_00_00,,,,,,,,,,,,,exception error");
        }

    }

    public static void build_ipevent_publc(int[] len_ipevent_public,
                                           byte[] buf_ipevent_public) {

        // ip event cmd
        try{
            byte[] buf_public = new byte[1460];
            byte[] buf = new byte[1460];

            int len_ip_event[] = new int[1];
            build_ip_event(len_ip_event, buf);
            int len_toacct = len_ip_event[0];
            // ///////////////////////////////////////////////////////////////////////////////////
            // private public cmd
            int len_public[] = new int[2];
            int length_private = len_ipevent_public[1];
            len_public[0] = len_toacct;
            len_public[1] = length_private;
            build_public(len_public, buf_public);
            System.arraycopy(buf_public, 0, buf, len_toacct + 21, 31);
            // //////////////////////////////////////////////////////////////////
            len_ipevent_public[0] = len_toacct;

            System.arraycopy(buf, 0, buf_ipevent_public, 0, len_toacct + 20 + 31);
        }
        catch(Exception e){
            logger.info("build_ipevent_publc,,,,,,,,,,,,,exception error");
        }


    }

    public static void build_ipevent_publc_machine_account(int[] len_ipevent_public,
                                           byte[] buf_ipevent_public,String str_machine_account) {

        // ip event cmd
        try{
            byte[] buf_public = new byte[1460];
            byte[] buf = new byte[1460];

            int len_ip_event[] = new int[1];
            build_ip_event_machine_account(len_ip_event, buf,str_machine_account);
            int len_toacct = len_ip_event[0];
            // ///////////////////////////////////////////////////////////////////////////////////
            // private public cmd
            int len_public[] = new int[2];
            int length_private = len_ipevent_public[1];
            len_public[0] = len_toacct;
            len_public[1] = length_private;
            build_public_machine_account(len_public, buf_public,str_machine_account);
            System.arraycopy(buf_public, 0, buf, len_toacct + 21, 31);
            // //////////////////////////////////////////////////////////////////
            len_ipevent_public[0] = len_toacct;

            System.arraycopy(buf, 0, buf_ipevent_public, 0, len_toacct + 20 + 31);
        }
        catch(Exception e){
            logger.info("build_ipevent_publc,,,,,,,,,,,,,exception error");
        }


    }

    public static void build_ip_event(int len_ip_event[], byte buf_ip_event[]) {
        try{
            byte[] buf_len = new byte[5];
            for (int i = 0; i < 5; i++) {
                buf_len[i] = 0;
            }
            int len = 0;
            byte buf[] = new byte[1460];
            buf[0] = 0x0A;// <LF>
            // crc final calc
            // 0LLL
            // ��id��
            String acct_id = "\"HENG-BO\"";
            int len_id = acct_id.length();
            // byte[] acct_id_temp = new byte[len_id];
            System.arraycopy(acct_id.getBytes(), 0, buf, 9, len_id);
            // seqence
            String acct_seq = "1111";
            int len_seq = acct_seq.length();
            // byte[] acct_seq_temp = new byte[len_seq];
            System.arraycopy(acct_seq.getBytes(), 0, buf, (9 + len_id), len_seq);
            // R123ABC
            String acct_Rrcvr = "R123ABC";
            int len_Rrcvr = acct_Rrcvr.length();
            byte[] acct_Rrcvr_temp = new byte[len_Rrcvr];
            System.arraycopy(acct_Rrcvr.getBytes(), 0, buf, (9 + len_id + len_seq),
                    len_Rrcvr);
            // L456DEF
            String acct_Lpref = "L456DEF#";
            int len_Lpref = acct_Lpref.length();
            byte[] acct_Lpref_temp = new byte[len_Lpref];
            System.arraycopy(acct_Lpref.getBytes(), 0, buf,
                    (9 + len_id + len_seq + len_Rrcvr), len_Lpref);
            // System.arraycopy((acct_Lpref.getBytes(),0,buf, (9 + len_id + len_seq
            // + len_Rrcvr), len_Lpref);
            // #13867802353
            String acct_machine = "8611112222" + "[]";
            int len_acct = acct_machine.length();
            byte[] acct_machine_temp = new byte[len_acct];
            System.arraycopy(acct_machine.getBytes(), 0, buf, (9 + len_id + len_seq
                    + len_Rrcvr + len_Lpref), len_acct);
            int len_toacct = 0;
            len_toacct = 9 + len_id + len_seq + len_Rrcvr + len_Lpref + len_acct;
            // //////////////////////////////////////////////////////////////////////////////
            if (true) {
                Calendar now = Calendar.getInstance();
                long srcTime = now.getTimeInMillis()
                        - TimeZone.getDefault().getRawOffset();
                String pat1 = "_HH:mm:ss,MM-dd-yyyy";
//                String pat1 = "HH:mm:ss-MM-dd-yyyy";
                SimpleDateFormat sdf1 = new SimpleDateFormat(pat1);
                Date date=new Date();
//                Date d = new Date(srcTime);
//                try {
//                    d = sdf1.parse(pat1);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                String str_time = sdf1.format(date);
                //logger.info("build_ip_event,str_time=" + str_time);
                System.arraycopy(str_time.getBytes(), 0, buf, len_toacct, 20);
            } else {

            }
            int i;

            // final calc 0LLL
            len = len_toacct + 20 - 9;
            udp_cmd.NumToHex_4(len, buf_len);
            for (i = 0; i < 4; i++) {
                buf[5 + i] = buf_len[i];
            }
            // final calc CRC
            byte[] buf_2 = new byte[len];
            System.arraycopy(buf, 9, buf_2, 0, len);
            int crc_value = udp_cmd.calcCRC_buf(buf_2, len);
            udp_cmd.NumToHex_4(crc_value, buf_len);
            for (i = 0; i < 4; i++) {
                buf[1 + i] = buf_len[i];
            }
            buf[len_toacct + 20] = 0x0D;// <CR>

            System.arraycopy(buf, 0, buf_ip_event, 0, len_toacct + 20 + 1);
            len_ip_event[0] = len_toacct;
        }
        catch(Exception e){
            logger.info("build_ip_event,,,,,,,,,,,,,exception error");
        }


    }
    public static void build_ip_event_machine_account(int len_ip_event[], byte buf_ip_event[],String str_machine_account) {
        try{
            byte[] buf_len = new byte[5];
            for (int i = 0; i < 5; i++) {
                buf_len[i] = 0;
            }
            int len = 0;
            byte buf[] = new byte[1460];
            buf[0] = 0x0A;// <LF>
            // crc final calc
            // 0LLL
            // ��id��
            String acct_id = "\"HENG-BO\"";
            int len_id = acct_id.length();
            // byte[] acct_id_temp = new byte[len_id];
            System.arraycopy(acct_id.getBytes(), 0, buf, 9, len_id);
            // seqence
            String acct_seq = "1111";
            int len_seq = acct_seq.length();
            // byte[] acct_seq_temp = new byte[len_seq];
            System.arraycopy(acct_seq.getBytes(), 0, buf, (9 + len_id), len_seq);
            // R123ABC
            String acct_Rrcvr = "R123ABC";
            int len_Rrcvr = acct_Rrcvr.length();
            byte[] acct_Rrcvr_temp = new byte[len_Rrcvr];
            System.arraycopy(acct_Rrcvr.getBytes(), 0, buf, (9 + len_id + len_seq),
                    len_Rrcvr);
            // L456DEF
            String acct_Lpref = "L456DEF#";
            int len_Lpref = acct_Lpref.length();
            byte[] acct_Lpref_temp = new byte[len_Lpref];
            System.arraycopy(acct_Lpref.getBytes(), 0, buf,
                    (9 + len_id + len_seq + len_Rrcvr), len_Lpref);
            // System.arraycopy((acct_Lpref.getBytes(),0,buf, (9 + len_id + len_seq
            // + len_Rrcvr), len_Lpref);
            // #13867802353
            String acct_machine = str_machine_account + "[]";
            int len_acct = acct_machine.length();
            byte[] acct_machine_temp = new byte[len_acct];
            System.arraycopy(acct_machine.getBytes(), 0, buf, (9 + len_id + len_seq
                    + len_Rrcvr + len_Lpref), len_acct);
            int len_toacct = 0;
            len_toacct = 9 + len_id + len_seq + len_Rrcvr + len_Lpref + len_acct;
            // //////////////////////////////////////////////////////////////////////////////
            if (true) {
                Calendar now = Calendar.getInstance();
                long srcTime = now.getTimeInMillis()
                        - TimeZone.getDefault().getRawOffset();
                String pat1 = "_HH:mm:ss,MM-dd-yyyy";
//                String pat1 = "HH:mm:ss-MM-dd-yyyy";
                SimpleDateFormat sdf1 = new SimpleDateFormat(pat1);
                Date date=new Date();
//                Date d = new Date(srcTime);
//                try {
//                    d = sdf1.parse(pat1);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                String str_time = sdf1.format(date);
                //logger.info("build_ip_event,str_time=" + str_time);
                System.arraycopy(str_time.getBytes(), 0, buf, len_toacct, 20);
            } else {

            }
            int i;

            // final calc 0LLL
            len = len_toacct + 20 - 9;
            udp_cmd.NumToHex_4(len, buf_len);
            for (i = 0; i < 4; i++) {
                buf[5 + i] = buf_len[i];
            }
            // final calc CRC
            byte[] buf_2 = new byte[len];
            System.arraycopy(buf, 9, buf_2, 0, len);
            int crc_value = udp_cmd.calcCRC_buf(buf_2, len);
            udp_cmd.NumToHex_4(crc_value, buf_len);
            for (i = 0; i < 4; i++) {
                buf[1 + i] = buf_len[i];
            }
            buf[len_toacct + 20] = 0x0D;// <CR>

            System.arraycopy(buf, 0, buf_ip_event, 0, len_toacct + 20 + 1);
            len_ip_event[0] = len_toacct;
        }
        catch(Exception e){
            logger.info("build_ip_event,,,,,,,,,,,,,exception error");
        }


    }

    public static void build_public(int len_public[], byte buf_public[]) {
        try{
            byte buf[] = new byte[1460];
            // /////////////////////////////////////////////
            int length_private = len_public[1];
            int len_toacct = len_public[0];
            buf[len_toacct + 21] = (byte) ((length_private >> 8) & 0xff);
            buf[len_toacct + 22] = (byte) (length_private & 0xff);
            // amarm machine account
            // Cstatic_var::NumCStringToHexByte((char *)"FFFFFFFFFFFF", (char
            // *)&buf[len_toacct + 23]);
            byte[] buf_machine_acct = new byte[9];
            NumCStringToHexByte("8611112222", buf_machine_acct);
            System.arraycopy(buf_machine_acct, 0, buf, (len_toacct + 23), 9);
            // alarm machine password
            byte[] buf_machine_psw = new byte[4];
            //logger.info("pws...............................................,static_var.machine_psw_static="
            //+ static_var.machine_psw_static);
            NumCStringToHexByte_8("123456", buf_machine_psw);
            System.arraycopy(buf_machine_psw, 0, buf, (len_toacct + 23 + 9), 4);
            /*
             * buf[len_toacct + 32] = '0'; buf[len_toacct + 33] = '0';
             * buf[len_toacct + 34] = '0'; buf[len_toacct + 35] = '0';
             */
            byte[] buf_phone_acct = new byte[9];
            NumCStringToHexByte("15934928273", buf_phone_acct);
            System.arraycopy(buf_phone_acct, 0, buf, (len_toacct + 36), 9);
            // object level
            int len_tolevel = len_toacct + 9 + 9 - 1 + 4 + 23;
            buf[len_tolevel + 1] = 2;
            // alarm center ip address and port number
            buf[len_tolevel + 2] = 0;
            buf[len_tolevel + 3] = 0;
            buf[len_tolevel + 4] = 0;
            buf[len_tolevel + 5] = 0;
            buf[len_tolevel + 6] = 0;
            buf[len_tolevel + 7] = 0;
            // ////////////////////////////////////////////////////////
            System.arraycopy(buf, len_toacct + 21, buf_public, 0, 31);
        }
        catch(Exception e){
            logger.info("build_public,,,,,,,,,,,,,exception error");
        }


    }
    public static void build_public_machine_account(int len_public[], byte buf_public[],String str_machine_account) {
        try{
            byte buf[] = new byte[1460];
            // /////////////////////////////////////////////
            int length_private = len_public[1];
            int len_toacct = len_public[0];
            buf[len_toacct + 21] = (byte) ((length_private >> 8) & 0xff);
            buf[len_toacct + 22] = (byte) (length_private & 0xff);
            // amarm machine account
            // Cstatic_var::NumCStringToHexByte((char *)"FFFFFFFFFFFF", (char
            // *)&buf[len_toacct + 23]);
            byte[] buf_machine_acct = new byte[9];
            NumCStringToHexByte(str_machine_account, buf_machine_acct);
            System.arraycopy(buf_machine_acct, 0, buf, (len_toacct + 23), 9);
            // alarm machine password
            byte[] buf_machine_psw = new byte[4];
            //logger.info("pws...............................................,static_var.machine_psw_static="
            //+ static_var.machine_psw_static);
            NumCStringToHexByte_8("123456", buf_machine_psw);
            System.arraycopy(buf_machine_psw, 0, buf, (len_toacct + 23 + 9), 4);
            /*
             * buf[len_toacct + 32] = '0'; buf[len_toacct + 33] = '0';
             * buf[len_toacct + 34] = '0'; buf[len_toacct + 35] = '0';
             */
            byte[] buf_phone_acct = new byte[9];
            NumCStringToHexByte("15934928273", buf_phone_acct);
            System.arraycopy(buf_phone_acct, 0, buf, (len_toacct + 36), 9);
            // object level
            int len_tolevel = len_toacct + 9 + 9 - 1 + 4 + 23;
            buf[len_tolevel + 1] = 2;
            // alarm center ip address and port number
            buf[len_tolevel + 2] = 0;
            buf[len_tolevel + 3] = 0;
            buf[len_tolevel + 4] = 0;
            buf[len_tolevel + 5] = 0;
            buf[len_tolevel + 6] = 0;
            buf[len_tolevel + 7] = 0;
            // ////////////////////////////////////////////////////////
            System.arraycopy(buf, len_toacct + 21, buf_public, 0, 31);
        }
        catch(Exception e){
            logger.info("build_public,,,,,,,,,,,,,exception error");
        }


    }

    public static void NumCStringToHexByte(String phone, byte[] buf) {
        try{
            String phone2 = "";
            phone2 = phone2.copyValueOf(phone.toCharArray());
            // logger.info(phone2);
            int len = phone2.length();
            boolean bAllNums = true;

            byte[] phone_byte = new byte[18];
            System.arraycopy(phone2.getBytes(), 0, phone_byte, 0, phone2.length());
            int i;
            byte ch = 0;
            for (i = 0; i < len; i++) {
                ch = phone_byte[i];
                if (ch < 48 || ch > 57) {
                    bAllNums = false;
                    break;
                }
            }

            if (bAllNums) {
                while (len < 18) {
                    phone2 = phone2 + "f";
                    len = phone2.length();
                }
                byte[] phone_byte2 = new byte[18];
                System.arraycopy(phone2.getBytes(), 0, phone_byte2, 0,
                        phone2.length());
                byte num = 0;

                byte[] hex = new byte[9];
                int ii;
                byte ch2 = 0;
                for (ii = 0; ii < len; ii++) {
                    ch2 = phone_byte2[ii];
                    if (ch2 == 'f') {
                        num = (byte) 0xff;
                    } else {
                        num = (byte) (ch2 - 48);
                    }
                    if (ii % 2 == 0) {
                        hex[ii / 2] = (byte) (0xf0 & (num << 4));
                    } else {
                        hex[ii / 2] |= (byte) (0x0f & num);
                    }
                }
                System.arraycopy(hex, 0, buf, 0, hex.length);
            } else {
                System.arraycopy("fffffffff".getBytes(), 0, buf, 0, 9);
            }
        }
        catch(Exception e){
            logger.info("NumCStringToHexByte,,,,,,,,,,,,,exception error");
        }

    }

    public static String HexByteToNumString( byte[] buf,int len) {
        try{
            int i;
            String string = "";
            byte num, high, low;
            for(i = 0; i < len; i++)
            {
                num = (byte)buf[i];
                high = (byte)(0x0f & (num >> 4));
                low = (byte)(0x0f & num);

                if(high == 0x0f)
                {
                    break;
                }
                else{
                    string = string + String.valueOf(high);
                }

                if(low == 0x0f)
                {
                    break;
                }
                else{
                    string = string + String.valueOf(low);
                }
            }
            return string;
        }
        catch(Exception e){
            logger.info("HexByteToNumString,,,,,,,,,,,,,exception error");
        }
        return "";


    }
    public static void NumCStringToHexByte_8(String phone, byte[] buf) {
        try{
            String phone2 = "";
            phone2 = phone2.copyValueOf(phone.toCharArray());
            // logger.info(phone2);
            int len = phone2.length();
            boolean bAllNums = true;

            byte[] phone_byte = new byte[8];
            System.arraycopy(phone2.getBytes(), 0, phone_byte, 0, phone2.length());
            int i;
            byte ch = 0;
            for (i = 0; i < len; i++) {
                ch = phone_byte[i];
                if (ch < 48 || ch > 57) {
                    bAllNums = false;
                    break;
                }
            }

            if (bAllNums) {
                while (len < 8) {
                    phone2 = phone2 + "f";
                    len = phone2.length();
                }
                byte[] phone_byte2 = new byte[8];
                System.arraycopy(phone2.getBytes(), 0, phone_byte2, 0,
                        phone2.length());
                byte num = 0;

                byte[] hex = new byte[4];
                int ii;
                byte ch2 = 0;
                for (ii = 0; ii < len; ii++) {
                    ch2 = phone_byte2[ii];
                    if (ch2 == 'f') {
                        num = (byte) 0xff;
                    } else {
                        num = (byte) (ch2 - 48);
                    }
                    if (ii % 2 == 0) {
                        hex[ii / 2] = (byte) (0xf0 & (num << 4));
                    } else {
                        hex[ii / 2] |= (byte) (0x0f & num);
                    }
                }
                System.arraycopy(hex, 0, buf, 0, hex.length);
            } else {
                System.arraycopy("fffffffff".getBytes(), 0, buf, 0, 4);
            }
        }
        catch(Exception e){
            logger.info("NumCStringToHexByte_8,,,,,,,,,,,,,exception error");
        }

    }

    public static void NumCStringToHexByte_6(String phone, byte[] buf) {
        try{
            String phone2 = "";
            phone2 = phone2.copyValueOf(phone.toCharArray());
            // logger.info(phone2);
            int len = phone2.length();
            boolean bAllNums = true;

            byte[] phone_byte = new byte[6];
            System.arraycopy(phone2.getBytes(), 0, phone_byte, 0, phone2.length());
            int i;
            byte ch = 0;
            for (i = 0; i < len; i++) {
                ch = phone_byte[i];
                if (ch < 48 || ch > 57) {
                    bAllNums = false;
                    break;
                }
            }

            if (bAllNums) {
                while (len < 6) {
                    phone2 = phone2 + "f";
                    len = phone2.length();
                }
                byte[] phone_byte2 = new byte[6];
                System.arraycopy(phone2.getBytes(), 0, phone_byte2, 0,
                        phone2.length());
                byte num = 0;

                byte[] hex = new byte[3];
                int ii;
                byte ch2 = 0;
                for (ii = 0; ii < len; ii++) {
                    ch2 = phone_byte2[ii];
                    if (ch2 == 'f') {
                        num = (byte) 0xff;
                    } else {
                        num = (byte) (ch2 - 48);
                    }
                    if (ii % 2 == 0) {
                        hex[ii / 2] = (byte) (0xf0 & (num << 4));
                    } else {
                        hex[ii / 2] |= (byte) (0x0f & num);
                    }
                }
                System.arraycopy(hex, 0, buf, 0, hex.length);
            } else {
                System.arraycopy("fffffffff".getBytes(), 0, buf, 0, 3);
            }
        }
        catch(Exception e){
            logger.info("NumCStringToHexByte_6,,,,,,,,,,,,,exception error");
        }

    }
    public static void NumCStringToHexByte_4(String phone, byte[] buf) {
        try{
            String phone2 = "";
            phone2 = phone2.copyValueOf(phone.toCharArray());
            // logger.info(phone2);
            int len = phone2.length();
            boolean bAllNums = true;

            byte[] phone_byte = new byte[4];
            System.arraycopy(phone2.getBytes(), 0, phone_byte, 0, phone2.length());
            int i;
            byte ch = 0;
            for (i = 0; i < len; i++) {
                ch = phone_byte[i];
                if (ch < 48 || ch > 57) {
                    bAllNums = false;
                    break;
                }
            }

            if (bAllNums) {
                while (len < 4) {
                    phone2 = phone2 + "f";
                    len = phone2.length();
                }
                byte[] phone_byte2 = new byte[4];
                System.arraycopy(phone2.getBytes(), 0, phone_byte2, 0,
                        phone2.length());
                byte num = 0;

                byte[] hex = new byte[2];
                int ii;
                byte ch2 = 0;
                for (ii = 0; ii < len; ii++) {
                    ch2 = phone_byte2[ii];
                    if (ch2 == 'f') {
                        num = (byte) 0xff;
                    } else {
                        num = (byte) (ch2 - 48);
                    }
                    if (ii % 2 == 0) {
                        hex[ii / 2] = (byte) (0xf0 & (num << 4));
                    } else {
                        hex[ii / 2] |= (byte) (0x0f & num);
                    }
                }
                System.arraycopy(hex, 0, buf, 0, hex.length);
            } else {
                System.arraycopy("fffffffff".getBytes(), 0, buf, 0, 3);
            }
        }
        catch(Exception e){
            logger.info("NumCStringToHexByte_4,,,,,,,,,,,,,exception error");
        }

    }

    public static void send_cmd_to_server(final byte[] cmd,String ip_port) {
        int len_total = 0;
        try {
            boolean bflag = false;
            int len_std_a = 0;
            int len_std_b = 0;
            if (cmd[7] < 58) {
                len_std_a = cmd[7] - 48;
            } else if ((cmd[7] >= 65) && (cmd[7] <= 70)) {
                len_std_a = cmd[7] - 55;
            }
            if (cmd[8] < 58) {
                len_std_b = cmd[8] - 48;
            } else if ((cmd[8] >= 65) && (cmd[7] <= 70)) {
                len_std_b = cmd[8] - 55;
            }
            int len_standard = len_std_a * 16 + len_std_b;
            int len_pri = cmd[len_standard + 10] * 256
                    + (int)(cmd[len_standard + 10 + 1] & 0xff);
            if(len_std_a < 0){
                logger.info("len_std_a=" + len_std_a);
                logger.info("send_cmd_to_server,error cmd,.....................................!");
                bflag = true;

            }
            if(len_std_b < 0){
                logger.info("len_std_b=" + len_std_b);
                logger.info("error cmd,.....................................!");
                bflag = true;

            }

            if((len_standard > 115) || (len_standard < 50))
            {
                logger.info("exception_handle_cmd_format_detail,len_standard=" + len_standard);
                logger.info("send_cmd_to_server,error cmd,.....................................!");
                bflag = true;


            }

            if((len_pri > 1024) || (len_pri < 31))
            {
                logger.info("exception_handle_cmd_format_detail,len_pri=" + len_pri);
                logger.info("send_cmd_to_server,error cmd,.....................................!");
                bflag = true;


            }

            len_total = len_standard + len_pri + 10 + 2 + 4;

//            logger.info("len_total=" + len_total);
            int len_temp = len_standard + 31 + 10;
            ///////////////////////////////////////////////////////////////
            long conn_number = (((byte)(cmd[len_temp + 2]) & 0xff)) * 65536 + (((byte)(cmd[len_temp + 3]) & 0xff))
                    * 256 + (((byte)(cmd[len_temp + 4]) & 0xff));

            int big_class = cmd[len_temp];
            int small_class = cmd[len_temp + 1];




//            ByteBuffer writeBuffer = ByteBuffer.allocateDirect(8192);
//            writeBuffer.clear();
            ByteBuffer writeBuffer=byte2Byffer(cmd,len_total);

            //final byte[] cmd2 = new byte[10];
            final int len_total2 = len_total;
//            int len = udp_cmd.socket_temp.write(writeBuffer);
            int len = static_var.map_socket.get(ip_port).write(writeBuffer);
//            logger.info("len=" + String.valueOf(len));



        } catch (Exception e) {
            Log.e("send_cmd_to_server=" + e.toString());
            // TODO Auto-generated catch block
            logger.info("send_cmd_to_server,len_total=" + len_total);
            logger.info("send_cmd_to_server,,,,,,,,,,,,,exception error");
        }


    }


    public static void send_cmd_to_server2(final byte[] cmd,String ip_port,String str_machine_acct) {
        int len_total = 0;
        try {
            boolean bflag = false;
            int len_std_a = 0;
            int len_std_b = 0;
            if (cmd[7] < 58) {
                len_std_a = cmd[7] - 48;
            } else if ((cmd[7] >= 65) && (cmd[7] <= 70)) {
                len_std_a = cmd[7] - 55;
            }
            if (cmd[8] < 58) {
                len_std_b = cmd[8] - 48;
            } else if ((cmd[8] >= 65) && (cmd[7] <= 70)) {
                len_std_b = cmd[8] - 55;
            }
            int len_standard = len_std_a * 16 + len_std_b;
            int len_pri = cmd[len_standard + 10] * 256
                    + (int)(cmd[len_standard + 10 + 1] & 0xff);
            if(len_std_a < 0){
                logger.info("len_std_a=" + len_std_a);
                logger.info("send_cmd_to_server,error cmd,.....................................!");
                bflag = true;

            }
            if(len_std_b < 0){
                logger.info("len_std_b=" + len_std_b);
                logger.info("error cmd,.....................................!");
                bflag = true;

            }

            if((len_standard > 115) || (len_standard < 50))
            {
                logger.info("exception_handle_cmd_format_detail,len_standard=" + len_standard);
                logger.info("send_cmd_to_server,error cmd,.....................................!");
                bflag = true;


            }

            if((len_pri > 1024) || (len_pri < 31))
            {
                logger.info("exception_handle_cmd_format_detail,len_pri=" + len_pri);
                logger.info("send_cmd_to_server,error cmd,.....................................!");
                bflag = true;


            }

            len_total = len_standard + len_pri + 10 + 2 + 4;

//            logger.info("len_total=" + len_total);
            int len_temp = len_standard + 31 + 10;
            ///////////////////////////////////////////////////////////////
            long conn_number = (((byte)(cmd[len_temp + 2]) & 0xff)) * 65536 + (((byte)(cmd[len_temp + 3]) & 0xff))
                    * 256 + (((byte)(cmd[len_temp + 4]) & 0xff));

            int big_class = cmd[len_temp];
            int small_class = cmd[len_temp + 1];




//            ByteBuffer writeBuffer = ByteBuffer.allocateDirect(8192);
//            writeBuffer.clear();
            ByteBuffer writeBuffer=byte2Byffer(cmd,len_total);

            //final byte[] cmd2 = new byte[10];
            final int len_total2 = len_total;
//            int len = udp_cmd.socket_temp.write(writeBuffer);
            int len = static_var.map_socket.get(ip_port).write(writeBuffer);
            if (str_machine_acct.equals("866569060902449")) {
                logger.info("send_cmd_to_server2,len=" + len + ",str_acct_machine=" + str_machine_acct);
//                                Cmd_fun.print_cmd(cmd);
            }
//            logger.info("len=" + String.valueOf(len));



        } catch (Exception e) {
            Log.e("send_cmd_to_server=" + e.toString());
            // TODO Auto-generated catch block
            logger.info("send_cmd_to_server,len_total=" + len_total);
            logger.info("send_cmd_to_server,,,,,,,,,,,,,exception error");
        }


    }

    /**
     * byte 数组转byteBuffer
     * @param byteArray
     */
    public static ByteBuffer byte2Byffer(byte[] byteArray,int len) {

        //初始化一个和byte长度一样的buffer
        byte[] buf_temp = new byte[len];
        System.arraycopy(byteArray, 0, buf_temp, 0, len);
        ByteBuffer buffer=ByteBuffer.allocate(len);
        // 数组放到buffer中
        buffer.put(buf_temp);
        //重置 limit 和postion 值 否则 buffer 读取数据不对
        buffer.flip();
        return buffer;
    }

    public static void event_cid_tostring_event_code_complete(byte[] cmd,
                                                              String str_event_code[]) {
        try{
            byte[] buf_len = new byte[5];
            for (int i = 0; i < 5; i++) {
                buf_len[i] = 0;
            }
            int len_std_a = 0;
            int len_std_b = 0;
            if (cmd[7] < 58) {
                len_std_a = cmd[7] - 48;
            } else if ((cmd[7] >= 65) && (cmd[7] <= 70)) {
                len_std_a = cmd[7] - 55;
            }
            if (cmd[8] < 58) {
                len_std_b = cmd[8] - 48;
            } else if ((cmd[8] >= 65) && (cmd[7] <= 70)) {
                len_std_b = cmd[8] - 55;
            }
            int len_standard = len_std_a * 16 + len_std_b;
            int len_pri = cmd[len_standard + 10] * 256 + (int) (cmd[len_standard + 10 + 1] & 0xff);
            int len_total = len_standard + len_pri + 10 + 2 + 4;
            // printf("len_standard=%d,len_pri=%d,len_total=%d\n",len_standard,len_pri,len_total);
            // logger.info("www=" + len_standard+ " " + len_pri+ " " + len_total);
            int len_temp = len_standard + 31 + 10;
            // ////////////////////////////////////////////////////////////////////////////////

            int big_class = cmd[len_temp];
            int small_class = cmd[len_temp + 1];
            big_class_2 = big_class;
            small_class_2 = small_class;


            int conn_number = cmd[len_temp + 2] * 65536 + cmd[len_temp + 3] * 256
                    + cmd[len_temp + 4];

            byte[] buf_acct_machine = new byte[9];
            System.arraycopy(cmd, len_standard + 12, buf_acct_machine, 0,
                    buf_acct_machine.length);
            String str_acct_machine = Cmd_fun.HexByteToNumString(
                    buf_acct_machine, (byte) buf_acct_machine.length);

            // /////////////////////////////////////////////////////////////////////////////
            byte[] buf_cid = new byte[19];
            System.arraycopy(cmd, len_standard + 10 - 20 - buf_cid.length - 1,
                    buf_cid, 0, buf_cid.length);

            byte[] buf_time = new byte[20];
            System.arraycopy(cmd, len_standard + 10 - 20 - 1,
                    buf_time, 0, buf_time.length);
            String str_time = new String(buf_time);
//            logger.info("str_time =" + str_time);
//            str_time =_10:14:13,11-28-2020

            String str_year = str_time.substring(16,20);
//            logger.info("str_year =" + str_year);
            String str_month = str_time.substring(10,12);
//            logger.info("str_month =" + str_month);
            String str_day = str_time.substring(13,15);
//            logger.info("str_day =" + str_day);

            String str_hour = str_time.substring(1,3);
//            logger.info("str_hour =" + str_hour);
            String str_minute = str_time.substring(4,6);
//            logger.info("str_minute =" + str_minute);
            String str_second = str_time.substring(7,9);
//            logger.info("str_second =" + str_second);

            String str_time_alarm_event = str_year + "-" + str_month + "-"  + str_day + " " + str_hour + ":"  + str_minute + ":"  + str_second;
//            logger.info("str_time_alarm_event =" + str_time_alarm_event);

            // ///////////////////////////////////////////////////////////////////////////////

            String str_data = new String(buf_cid);
//            logger.info("str_data =" + str_data);


            int index_left = str_data.indexOf("|");
            //logger.info("index_left=" + String.valueOf(index_left));

            byte[] buf_cid_pure = new byte[13];
            System.arraycopy(buf_cid, index_left, buf_cid_pure, 0,
                    buf_cid_pure.length);
            String str_data_pure = new String(buf_cid_pure);
//            logger.info("str_data_pure =" + str_acct_machine + "：" +str_data_pure);
            // ///////////////////////////////////////////////////////////////////////////////
            String str_zone_number = Cmd_fun
                    .event_cid_tostring_zone_number(buf_cid_pure);

            int aa = Integer.valueOf(str_zone_number).intValue();
            String str_zone_number_final = String.valueOf(aa);
            String str_event_code_three = Cmd_fun
                    .event_cid_tostring_event_code_three(buf_cid_pure);

            String str_event_Q = Cmd_fun.event_cid_tostring_event_Q(buf_cid_pure);

            if (str_zone_number.equals("")) {
                return;
            }
            str_event_code[0] = str_event_code_three;
            str_event_code[1] = str_zone_number_final;
            str_event_code[2] = str_event_Q;
            str_event_code[3] = str_data_pure;
            str_event_code[4] = str_time_alarm_event;
            str_event_code[5] = str_data;

        }
        catch(Exception e){
            logger.info("event_cid_tostring_event_code_complete,,,,,,,,,,,,,exception error");
        }

    }

    public static String event_cid_tostring_zone_number(byte[] buf) {
        try{
            String str = new String(buf);
            byte[] buf_zone_number = new byte[3];
            System.arraycopy(buf, 9, buf_zone_number, 0, buf_zone_number.length);
            String str_zone_number = new String(buf_zone_number);

            if (buf[0] == 0x7c)// "|"
            {
                return str_zone_number;
            }
        }
        catch(Exception e){
            logger.info("build_ip_event_04_04,,,,,,,,,,,,,exception error");
        }

        // return str_event_Q;
        return "";
    }

    public static String event_cid_tostring_event_code_three(byte[] buf) {
        try{
            String str = new String(buf);
            //logger.info("event_cid_tostring_event_code   " + str);
            String str_event_code = str.substring(2, 5);
            String str_event_Q = str.substring(1, 2);
            String str_event_brace = str.substring(0, 1);
            //logger.info(str_event_code);
            //logger.info(str_event_Q);
            //logger.info(str_event_brace);
            return str_event_code;


        }
        catch(Exception e){
            logger.info("event_cid_tostring_event_code,,,,,,,,,,,,,exception error");
        }


        // return str_event_Q;
        return "";
    }
    public static String event_cid_tostring_event_code_chinese_or_english(String str_event_Q,String str_event_code,String str_language) {
        try{

            //logger.info(str_event_code);
            //logger.info(str_event_Q);


            if (str_language.equals("China")) {
                if (str_event_code.equals("110")) {
                    return static_var.str_110_var;
                } else if (str_event_code.equals("131")) {
                    return static_var.str_131_var;
                }else if (str_event_code.equals("120")) {
                    return static_var.str_120_var;

                } else if (str_event_code.equals("130")) {
                    return static_var.str_130_var;
                } else if (str_event_code.equals("121")) {
                    return static_var.str_121_var;
                } else if (str_event_code.equals("151")) {
                    return static_var.str_151_var;
                } else if (str_event_code.equals("113")) {
                    return static_var.str_113_var;
                } else if (str_event_code.equals("456")) {
                    return static_var.str_456_var;
                } else if (str_event_code.equals("380")) {
                    return static_var.str_380_var;
                } else if (str_event_code.equals("384")) {
                    return static_var.str_384_var;
                } else if (str_event_code.equals("383")) {
                    return static_var.str_383_var;
                }else if (str_event_code.equals("710")) {
                    return static_var.str_1710_var;
                } else if (str_event_code.equals("711")) {
                    return static_var.str_1711_var;
                } else if (str_event_code.equals("400")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1400_var;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3400_var;
                    }
                }else if (str_event_code.equals("301")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1301_var;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3301_var;
                    }
                }else if (str_event_code.equals("302")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1302_var;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3302_var;
                    }
                }
            } else if (str_language.equals("English")) {
                if (str_event_code.equals("110")) {
                    return static_var.str_110_var_en;
                } else if (str_event_code.equals("131")) {
                    return static_var.str_131_var_en;
                }else if (str_event_code.equals("120")) {
                    return static_var.str_120_var_en;

                } else if (str_event_code.equals("130")) {
                    return static_var.str_130_var_en;
                } else if (str_event_code.equals("121")) {
                    return static_var.str_121_var_en;
                } else if (str_event_code.equals("151")) {
                    return static_var.str_151_var_en;
                } else if (str_event_code.equals("113")) {
                    return static_var.str_113_var_en;
                } else if (str_event_code.equals("456")) {
                    return static_var.str_456_var_en;
                } else if (str_event_code.equals("380")) {
                    return static_var.str_380_var_en;
                } else if (str_event_code.equals("384")) {
                    return static_var.str_384_var_en;
                } else if (str_event_code.equals("383")) {
                    return static_var.str_383_var_en;
                }else if (str_event_code.equals("400")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1400_var_en;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3400_var_en;
                    }
                }else if (str_event_code.equals("301")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1301_var_en;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3301_var_en;
                    }
                }else if (str_event_code.equals("302")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1302_var_en;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3302_var_en;
                    }
                }
            }else if (str_language.equals("German")) {
                if (str_event_code.equals("110")) {
                    return static_var.str_110_var_german;
                } else if (str_event_code.equals("131")) {
                    return static_var.str_131_var_german;
                }else if (str_event_code.equals("120")) {
                    return static_var.str_120_var_german;
                } else if (str_event_code.equals("130")) {
                    return static_var.str_130_var_german;
                } else if (str_event_code.equals("121")) {
                    return static_var.str_121_var_german;
                } else if (str_event_code.equals("151")) {
                    return static_var.str_151_var_german;
                } else if (str_event_code.equals("113")) {
                    return static_var.str_113_var_german;
                } else if (str_event_code.equals("456")) {
                    return static_var.str_456_var_german;
                } else if (str_event_code.equals("380")) {
                    return static_var.str_380_var_german;
                } else if (str_event_code.equals("384")) {
                    return static_var.str_384_var_german;
                } else if (str_event_code.equals("383")) {
                    return static_var.str_383_var_german;
                }else if (str_event_code.equals("400")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1400_var_german;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3400_var_german;
                    }
                }else if (str_event_code.equals("301")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1301_var_german;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3301_var_german;
                    }
                }else if (str_event_code.equals("302")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1302_var_german;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3302_var_german;
                    }
                }
            }else if (str_language.equals("Russian")) {
                if (str_event_code.equals("110")) {
                    return static_var.str_110_var_russian;
                } else if (str_event_code.equals("131")) {
                    return static_var.str_131_var_russian;
                }else if (str_event_code.equals("120")) {
                    return static_var.str_120_var_russian;
                } else if (str_event_code.equals("130")) {
                    return static_var.str_130_var_russian;
                } else if (str_event_code.equals("121")) {
                    return static_var.str_121_var_russian;
                } else if (str_event_code.equals("151")) {
                    return static_var.str_151_var_russian;
                } else if (str_event_code.equals("113")) {
                    return static_var.str_113_var_russian;
                } else if (str_event_code.equals("456")) {
                    return static_var.str_456_var_russian;
                } else if (str_event_code.equals("380")) {
                    return static_var.str_380_var_russian;
                } else if (str_event_code.equals("384")) {
                    return static_var.str_384_var_russian;
                } else if (str_event_code.equals("383")) {
                    return static_var.str_383_var_russian;
                }else if (str_event_code.equals("400")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1400_var_russian;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3400_var_russian;
                    }
                }else if (str_event_code.equals("301")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1301_var_russian;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3301_var_russian;
                    }
                }else if (str_event_code.equals("302")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1302_var_russian;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3302_var_russian;
                    }
                }
            }else if (str_language.equals("French")) {
                if (str_event_code.equals("110")) {
                    return static_var.str_110_var_french;
                } else if (str_event_code.equals("131")) {
                    return static_var.str_131_var_french;
                }else if (str_event_code.equals("120")) {
                    return static_var.str_120_var_french;
                } else if (str_event_code.equals("130")) {
                    return static_var.str_130_var_french;
                } else if (str_event_code.equals("121")) {
                    return static_var.str_121_var_french;
                } else if (str_event_code.equals("151")) {
                    return static_var.str_151_var_french;
                } else if (str_event_code.equals("113")) {
                    return static_var.str_113_var_french;
                } else if (str_event_code.equals("456")) {
                    return static_var.str_456_var_french;
                } else if (str_event_code.equals("380")) {
                    return static_var.str_380_var_french;
                } else if (str_event_code.equals("384")) {
                    return static_var.str_384_var_french;
                } else if (str_event_code.equals("383")) {
                    return static_var.str_383_var_french;
                }else if (str_event_code.equals("400")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1400_var_french;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3400_var_french;
                    }
                }else if (str_event_code.equals("301")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1301_var_french;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3301_var_french;
                    }
                }else if (str_event_code.equals("302")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1302_var_french;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3302_var_french;
                    }
                }
            }else if (str_language.equals("Italian")) {
                if (str_event_code.equals("110")) {
                    return static_var.str_110_var_italian;
                } else if (str_event_code.equals("131")) {
                    return static_var.str_131_var_italian;
                }else if (str_event_code.equals("120")) {
                    return static_var.str_120_var_italian;
                } else if (str_event_code.equals("130")) {
                    return static_var.str_130_var_italian;
                } else if (str_event_code.equals("121")) {
                    return static_var.str_121_var_italian;
                } else if (str_event_code.equals("151")) {
                    return static_var.str_151_var_italian;
                } else if (str_event_code.equals("113")) {
                    return static_var.str_113_var_italian;
                } else if (str_event_code.equals("456")) {
                    return static_var.str_456_var_italian;
                } else if (str_event_code.equals("380")) {
                    return static_var.str_380_var_italian;
                } else if (str_event_code.equals("384")) {
                    return static_var.str_384_var_italian;
                } else if (str_event_code.equals("383")) {
                    return static_var.str_383_var_italian;
                }else if (str_event_code.equals("400")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1400_var_italian;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3400_var_italian;
                    }
                }else if (str_event_code.equals("301")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1301_var_italian;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3301_var_italian;
                    }
                }else if (str_event_code.equals("302")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1302_var_italian;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3302_var_italian;
                    }
                }
            }else if (str_language.equals("Spanish")) {
                if (str_event_code.equals("110")) {
                    return static_var.str_110_var_spanish;
                } else if (str_event_code.equals("131")) {
                    return static_var.str_131_var_spanish;
                }else if (str_event_code.equals("120")) {
                    return static_var.str_120_var_spanish;
                } else if (str_event_code.equals("130")) {
                    return static_var.str_130_var_spanish;
                } else if (str_event_code.equals("121")) {
                    return static_var.str_121_var_spanish;
                } else if (str_event_code.equals("151")) {
                    return static_var.str_151_var_spanish;
                } else if (str_event_code.equals("113")) {
                    return static_var.str_113_var_spanish;
                } else if (str_event_code.equals("456")) {
                    return static_var.str_456_var_spanish;
                } else if (str_event_code.equals("380")) {
                    return static_var.str_380_var_spanish;
                } else if (str_event_code.equals("384")) {
                    return static_var.str_384_var_spanish;
                } else if (str_event_code.equals("383")) {
                    return static_var.str_383_var_spanish;
                }else if (str_event_code.equals("400")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1400_var_spanish;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3400_var_spanish;
                    }
                }else if (str_event_code.equals("301")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1301_var_spanish;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3301_var_spanish;
                    }
                }else if (str_event_code.equals("302")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1302_var_spanish;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3302_var_spanish;
                    }
                }
            }else if (str_language.equals("Portuguese")) {
                if (str_event_code.equals("110")) {
                    return static_var.str_110_var_portuguese;
                } else if (str_event_code.equals("131")) {
                    return static_var.str_131_var_portuguese;
                }else if (str_event_code.equals("120")) {
                    return static_var.str_120_var_portuguese;
                } else if (str_event_code.equals("130")) {
                    return static_var.str_130_var_portuguese;
                } else if (str_event_code.equals("121")) {
                    return static_var.str_121_var_portuguese;
                } else if (str_event_code.equals("151")) {
                    return static_var.str_151_var_portuguese;
                } else if (str_event_code.equals("113")) {
                    return static_var.str_113_var_portuguese;
                } else if (str_event_code.equals("456")) {
                    return static_var.str_456_var_portuguese;
                } else if (str_event_code.equals("380")) {
                    return static_var.str_380_var_portuguese;
                } else if (str_event_code.equals("384")) {
                    return static_var.str_384_var_portuguese;
                } else if (str_event_code.equals("383")) {
                    return static_var.str_383_var_portuguese;
                }else if (str_event_code.equals("400")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1400_var_portuguese;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3400_var_portuguese;
                    }
                }else if (str_event_code.equals("301")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1301_var_portuguese;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3301_var_portuguese;
                    }
                }else if (str_event_code.equals("302")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1302_var_portuguese;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3302_var_portuguese;
                    }
                }
            } else if (str_language.equals("Arabic")) {
                if (str_event_code.equals("110")) {
                    return static_var.str_110_var_arabic;
                } else if (str_event_code.equals("131")) {
                    return static_var.str_131_var_arabic;
                }else if (str_event_code.equals("120")) {
                    return static_var.str_120_var_arabic;
                } else if (str_event_code.equals("130")) {
                    return static_var.str_130_var_arabic;
                } else if (str_event_code.equals("121")) {
                    return static_var.str_121_var_arabic;
                } else if (str_event_code.equals("151")) {
                    return static_var.str_151_var_arabic;
                } else if (str_event_code.equals("113")) {
                    return static_var.str_113_var_arabic;
                } else if (str_event_code.equals("456")) {
                    return static_var.str_456_var_arabic;
                } else if (str_event_code.equals("380")) {
                    return static_var.str_380_var_arabic;
                } else if (str_event_code.equals("384")) {
                    return static_var.str_384_var_arabic;
                } else if (str_event_code.equals("383")) {
                    return static_var.str_383_var_arabic;
                }else if (str_event_code.equals("400")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1400_var_arabic;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3400_var_arabic;
                    }
                }else if (str_event_code.equals("301")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1301_var_arabic;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3301_var_arabic;
                    }
                }else if (str_event_code.equals("302")) {
                    if (str_event_Q.equals("1")) {
                        return static_var.str_1302_var_arabic;
                    } else if (str_event_Q.equals("3")) {
                        return static_var.str_3302_var_arabic;
                    }
                }
            }



                return "";


        }
        catch(Exception e){
            logger.info("event_cid_tostring_event_code,,,,,,,,,,,,,exception error");
        }


        // return str_event_Q;
        return "";
    }

    public static String event_cid_tostring_event_Q(byte[] buf) {
        try{
            String str = new String(buf);
            //logger.info("event_cid_tostring_event_code   " + str);
            String str_event_code = str.substring(2, 5);
            String str_event_Q = str.substring(1, 2);
            String str_event_brace = str.substring(0, 1);
            //logger.info(str_event_code);
            //logger.info(str_event_Q);
            //logger.info(str_event_brace);
            if (!(str_event_Q.equals("1") || str_event_Q.equals("3"))) {
                return "";
            } else {
                return str_event_Q;
            }
        }
        catch(Exception e){
            logger.info("build_ip_event_04_04,,,,,,,,,,,,,exception error");
        }
        return "";

    }

    public static boolean cmd_analysis_1(byte[] cmd,ByteBuffer buffer) {


        try {
            j = 0;
            for (int k = 0; k < 1460; k++) {
                /*logger.info("end_position=" + buffer.position());
                logger.info("end_mark=" + buffer.mark());
                logger.info("end_limit=" + buffer.limit());
                logger.info("end_capacity=" + buffer.capacity());
                logger.info("end_remaining=" + buffer.remaining());*/
                //                    buffer.get(aaa);
                if ((buffer.position() > 1460) || (buffer.position() > buffer.limit())) {
                    logger.info("error,cmd_analysis_1,buffer.position=" + buffer.position());
                    return false;
                }
                if (buffer.remaining() < 1) {
                    logger.info("error,cmd_analysis_1,buffer.position=" + buffer.position());
                    return false;
                }
                buffer.get(array_1,0,1);//
                if (array_1[0] == '"')
                {
                    if (j >= 9)
                    {
                        System.arraycopy(array_temp, j - 9, array_front, 0, 9);
                        System.arraycopy(array_1, 0, array_temp, j, 1);//
                        j++;
                    }
                    //////////////////////////////////////////////////////////////////////////////
                    if ((buffer.position() > 1460) || (buffer.position() > buffer.limit())) {
                        logger.info("error,cmd_analysis_1,buffer.position=" + buffer.position());
                        return false;
                    }
                    if (buffer.remaining() < 3) {
                        logger.info("error,cmd_analysis_1,buffer.position=" + buffer.position());
                        return false;
                    }
                    buffer.get(array_3,0,3);//
//                            Cmd_fun.print_cmd_asc(array_3, 3);
                    String acct_id = "HEN";
                    String acct_id2 = "ACK";
                    String acct_id3 = "NUL";
                    boolean ret_cmp = Arrays.equals(array_3, acct_id.getBytes());
                    boolean ret_cmp2 = Arrays.equals(array_3, acct_id2.getBytes());
                    boolean ret_cmp3 = Arrays.equals(array_3, acct_id3.getBytes());
                    if(!(ret_cmp || ret_cmp2 || ret_cmp3))
                    {
                        Log.i("error,the cmd haven't HEN ACK NUL");
                        return false;
                    }
                    else
                    {
                        System.arraycopy(array_3, 0, array_temp, j, 3);
                        j = j + 3;
                        int len_standard = Cmd_fun.get_len_standard(array_front);
                        if (len_standard <= 0) {
                            logger.info("error,cmd_analysis_1,len_standard=" + len_standard);
                            return false;
                        }
//                                Log.i( "len_standard=" + len_standard);
                        ////////////////////////////////////////////////////////////////////
                        if (buffer.remaining() < (len_standard - 1)) {
                            logger.info("error,cmd_analysis_1,buffer.position=" + buffer.position());
                            return false;
                        }
                        buffer.get(array_temp_1,0,len_standard - 1);//
                        /*Cmd_fun.print_cmd_hex(array_temp_1,len_standard - 1);
                        Cmd_fun.print_cmd_asc(array_temp_1,len_standard - 1);*/
                        int len_standard_4 = len_standard - 1;
                        System.arraycopy(array_temp_1, 0, array_temp, j, len_standard_4);
//                        Cmd_fun.print_cmd_hex(array_temp,len_standard + 10 + 2);
//                        Cmd_fun.print_cmd_asc(array_temp,len_standard + 10 + 2);
                        j = j + len_standard_4;
                        int len_pri = array_temp[len_standard + 10] * 256 + (int)(array_temp[len_standard + 10 + 1] & 0xff);
                        int len_total = len_standard + len_pri + 10 + 2 + 4;
//                                Log.i("len_pri=" + len_pri + " len_total=" + len_total);
                        /////////////////////////////////////////////////////////////////////////
                        if ((len_pri > 1460) || (len_pri <= 0)) {
                            logger.info("error,cmd_analysis_1,len_pri=" + len_pri);
                            return false;
                        }
                        if (buffer.remaining() < (len_pri + 4)) {
                            logger.info("error,cmd_analysis_1,buffer.position=" + buffer.position());
                            return false;
                        }
                        buffer.get(array_temp_2,0,len_pri + 4);//
                        int len_pri_1 = len_pri + 4;
                        System.arraycopy(array_temp_2, 0,array_temp , j, len_pri + 4);
//                        Cmd_fun.print_cmd(array_temp);
//                        Cmd_fun.print_cmd_hex(array_temp,len_total);


                        j = 0;
                               /* logger.info("end_position=" + buffer.position());
                                logger.info("end_mark=" + buffer.mark());
                                logger.info("end_limit=" + buffer.limit());
                                logger.info("end_capacity=" + buffer.capacity());
                               logger.info("end_remaining=" + buffer.remaining());*/

                        logger.info("cmd_analysis_1，position=" + buffer.position());
                        logger.info("cmd_analysis_1，limit=" + buffer.limit());
                        logger.info("cmd_analysis_1，capacity=" + buffer.capacity());


                        if (buffer.position() == buffer.limit()) {
//                                    logger.info("The command has been processed......");
                            System.arraycopy(array_temp, 0,cmd , 0, len_total);
                            boolean bflag = Cmd_fun.exception_handle_cmd_format_detail(array_temp);
                            if(!bflag) {
                                return true;
                            }else{
                                return false;
                            }
                        } else if ((buffer.position() < buffer.limit()) && (buffer.position() > 0)) {
                            System.arraycopy(array_temp, 0,cmd , 0, len_total);
                            boolean bflag = Cmd_fun.exception_handle_cmd_format_detail(array_temp);
//                        logger.info("bflag=" ,String.valueOf(bflag));
                            if(!bflag) {
                                return true;
                            }else{
                                return false;
                            }
                        }else{
                            return false;
                        }

                    }
                }else{

                    if ((j >= 1460)) {
                        logger.info("error,cmd_analysis_1,j=" + j);
                        return false;
                    }
                    System.arraycopy(array_1, 0, array_temp, j, 1);//
//                            Cmd_fun.print_cmd_asc(array_1, 1);
                    j++;
                }
//                buffer.clear();
            }

        } catch (Exception e) {
            logger.info("cmd_analysis_1,error=",e);
            e.printStackTrace();
            j = 0;
            return false;
        }
        return false;
    }

    public static int get_len_standard(byte[] array_front) {
        try{
            int len_std_a = 0;
            int len_std_b = 0;
            if (array_front[7] < 58) {
                len_std_a = array_front[7] - 48;
            } else if ((array_front[7] >= 65) && (array_front[7] <= 70)) {
                len_std_a = array_front[7] - 55;
            }
            if (array_front[8] < 58) {
                len_std_b = array_front[8] - 48;
            } else if ((array_front[8] >= 65) && (array_front[7] <= 70)) {
                len_std_b = array_front[8] - 55;
            }
            return len_std_a * 16 + len_std_b;
        }
        catch(Exception e){
            logger.info("main,error=",e);
            e.printStackTrace();
        }
        return -1;
    }

    public static void print_cmd_asc(byte[] cmd, int len) {
        try{
            byte[] buf_len = new byte[5];
            for (int i = 0; i < 5; i++) {
                buf_len[i] = 0;
            }
            String str_ip_event = "";
            for (int i = 0; i < len; i++) {
                str_ip_event = str_ip_event + (char) cmd[i];
            }

            Log.i("data_all=" + str_ip_event);
        }
        catch(Exception e){
            Log.i("print_cmd,,,,,,,,,,,,,exception error");
        }

    }
}
